package com.summer.service.impl;

import com.summer.entity.OrderCollection;
import com.summer.entity.OrderCollectionDetail;
import com.summer.entity.OrderCollectionReduction;
import com.summer.mapper.OrderCollectionDAO;
import com.summer.mapper.OrderCollectionDetailDAO;
import com.summer.mapper.OrderCollectionReductionDAO;
import com.summer.mapper.OrderRepaymentMapper;
import com.summer.service.IOrderCollectionService;
import com.summer.variable.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Desc:
 * Created by tl on 2019/1/5
 */
@Service
public class OrderCollectionService implements IOrderCollectionService {
    private static Logger log = LoggerFactory.getLogger(OrderCollectionService.class);
    @Resource
    private OrderRepaymentMapper orderRepaymentMapper;
    @Resource
    private OrderCollectionReductionDAO orderCollectionReductionDAO;
    @Resource
    private OrderCollectionDetailDAO orderCollectionDetailDAO;
    @Resource
    private OrderCollectionDAO orderCollectionDAO;

    @Override
    public void reduce(Map<String, Object> params, OrderCollection orderCollection, int reductionAmount) {

        //TODO 操作员
        Date now = new Date();
        OrderCollectionReduction newReduction = new OrderCollectionReduction(orderCollection.getId(), 0,
                orderCollection.getUserId(), reductionAmount, Constant.COLLECTION_AUDIT_TODO, now, now, "");
        Object remarkObj = params.get("remark");
        if (remarkObj != null) {
            newReduction.setRemark(remarkObj.toString());
        }
        orderCollectionReductionDAO.insertSelective(newReduction);
    }

    @Override
    @Transactional
    public void addDetail(Map<String, Object> params, OrderCollection orderCollection) {
        Byte contactType = params.get("contactType") == null ? 0 : Byte.parseByte(params.get("contactType").toString());
        Byte relation = params.get("relation") == null ? 0 : Byte.parseByte(params.get("relation").toString());
        Byte collectionType = params.get("collectionType") == null ? 0 :
                Byte.parseByte(params.get("collectionType").toString());
        Object promiseRepaymentTimeObj = params.get("promiseRepaymentTime");
        String remark = params.get("remark") == null ? "" : params.get("remark").toString();
        String names = params.get("names") == null ? "" : params.get("names").toString();
        String contactPhone = params.get("contactPhone") == null ? "" : params.get("contactPhone").toString();
        String contactName = params.get("contactName") == null ? "" : params.get("contactName").toString();

        //TODO 操作员
        Date now = new Date();
        Integer id = orderCollection.getId();
        OrderCollectionDetail newOrder = new OrderCollectionDetail(id, 0,
                orderCollection.getUserId(), contactType, contactName, relation, contactPhone, (byte) 0,
                orderCollection.getStatus(), collectionType, 0, now, now, names, remark);
        OrderCollection newCollection = new OrderCollection();
        newCollection.setId(id);
        if (null != promiseRepaymentTimeObj) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = sdf.parse(promiseRepaymentTimeObj.toString());
                newCollection.setPromiseRepaymentTime(date);
            } catch (ParseException e) {
                log.error("OrderCollectionService addDetail dateParseError id=" + id);
            }
        }
        orderCollectionDAO.updateByPrimaryKeySelective(newCollection);
        orderCollectionDetailDAO.insertSelective(newOrder);
    }

    @Override
    public void dispatch(Integer platformUserId, OrderCollection orderCollection) {
        OrderCollection newCollection = new OrderCollection();
        newCollection.setId(orderCollection.getId());
        newCollection.setLastCollectionUserId(orderCollection.getCurrentCollectionUserId());
        newCollection.setCurrentCollectionUserId(platformUserId);
        orderCollectionDAO.updateByPrimaryKeySelective(newCollection);
    }
}
